<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        a {
            color: inherit;
            text-decoration: none;
        }

        .wrap {
            width: 1140px;
            margin: 0 auto;
        }

        .wrap .searchBar {
            margin-bottom: 20px;
            display: flex;
            height: 40px;
        }

        .wrap .searchCon {
            flex: 1;
            outline: none;
            text-indent: 10px;
        }

        .wrap .searchBtn {
            width: 60px;
            background-color: grey;
            text-align: center;
            line-height: 40px;
            border-radius: 4px;
        }

        .goodList li {
            width: 23%;
            height: 267px;
            padding: 0 10px;
            box-sizing: border-box;
            float: left;
            border: 1px solid #000;
            margin-bottom: 20px;
        }

        .goodList li:not(:nth-child(4n)) {
            margin-right: 2%;
        }

        .goodList li img {
            width: 90%;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <div class="searchBar">
            <input type="text" class="searchCon">
            <a href="javascript:;" class="searchBtn">搜索</a>
        </div>
        <ul class="goodList">


        </ul>
    </div>
    <script>
        var ul = document.querySelector(".goodList");
        var searchCon = document.querySelector(".searchCon");
        var searchBtn = document.querySelector(".searchBtn");

        var xhr = new XMLHttpRequest();
        xhr.open("get", "../data/json.json", true)
        xhr.send()
        xhr.addEventListener("readystatechange", function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var str = JSON.parse(xhr.responseText)
                console.log(str)
                var html = '';
                str.forEach(function (item) {
                    var { goodsImg, goodsMsg, goodsPrice } = item
                    // console.log({ goodsImg, goodsMsg, goodsPrice })
                    html += `
                    <li>
                        <a href="">
                            <img src="${goodsImg}" alt="">
                            <h3>${goodsMsg}</h3>
                            <p>${goodsPrice}</p>
                        </a>
                     </li>
                    `
                    ul.innerHTML = html
                })
                searchBtn.addEventListener("click", function () {
                    var strIpt = searchCon.value.trim()

                    var list = str.filter(function (item) {

                        var goodName = item.goodsName
                        var reg = new RegExp(strIpt, "i")
                        return reg.test(goodName)
                    })
                    console.log(list)
                    var html = '';
                    list.forEach(function (item) {
                        var { goodsImg, goodsMsg, goodsPrice } = item
                        html += `
                    <li>
                        <a href="">
                            <img src="${goodsImg}" alt="">
                            <h3>${goodsMsg}</h3>
                            <p>${goodsPrice}</p>
                        </a>
                     </li>
                    `
                        ul.innerHTML = html
                    })

                })

            }
        })
    </script>
</body>

</html>